package com.earnings.okhttputils.utils.https;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.earnings.okhttputils.utils.OkHttp.callback.Callback;
import com.earnings.okhttputils.utils.UrlConstant.Constant;
import com.earnings.okhttputils.utils.bean.BaseBean;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

import org.json.JSONArray;
import org.json.JSONException;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by HOHO on 2017/4/25.
 */

public abstract class HttpArrayCallback<T> extends Callback<String> {
    private Context context;
    public HttpArrayCallback(Context context){
        this.context=context;
    }
    @Override
    public String parseNetworkResponse(Response response, int id) throws Exception {
        Log.e(Constant.TAG,response.request().url().toString() );
        return response.body().string();
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        e.printStackTrace();
        Toast.makeText( context,Constant.MSG_ERROR ,Toast.LENGTH_SHORT).show();
        onFail(Constant.STATUS_ERROR,Constant.MSG_ERROR);
    }

    @Override
    public void onResponse(String response, int id) {
        Log.e( Constant.TAG,response.toString() );
        try {

        JsonGenericsSerializator json=new JsonGenericsSerializator();
        Gson gson =new Gson();
        BaseBean entity = json.transform(response,BaseBean.class);
        if (Constant.STATUS_SUCCESS==entity.getStatus()) {

            Class <T> entityClass = (Class <T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];//相当于T.class
            JsonArray jsonArray =null;
            if(entity.getResult()!=null) jsonArray =entity.getResult().getAsJsonArray();
        //    if(entity.getData()!=null) jsonArray =entity.getData().getAsJsonArray();

           // }
            Iterator it = jsonArray.iterator();
            List<T> data=new ArrayList<T>();
            while(it.hasNext()){
                JsonElement e = (JsonElement)it.next();
                //JsonElement转换为JavaBean对象
                   T   object = null;
                    if(entityClass==JSONArray.class)object= (T)new JSONArray(e.toString());
                    else if(entityClass==String.class)object= (T)(e.toString());
                    else object = gson.fromJson(e.toString(),entityClass);

                data.add(object);
            }

            onSuccess(data,entity.getMsg());

        }else {
            if(entity.getStatus()==Constant.STATUS_TOKEN) {
                //token不正确退出登录
            Toast.makeText( context, entity.getMsg(), Toast.LENGTH_SHORT ).show();

        }
            onFail(entity.getStatus(),entity.getMsg());
        }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public abstract void onSuccess(List<T> response, String msg);
    public abstract void onFail(int code,String msg);
}
